package datastructure;

/**
 * @author ZCR
 * @create 2025/8/28 21:20
 */
public class NumArray {
    int[] prefixSum;

    public NumArray(int[] nums) {
        prefixSum = new int[nums.length];
        for (int i = 0; i < nums.length; i++) {
            prefixSum[i] = i == 0 ? nums[0] : prefixSum[i - 1] + nums[i];
        }
    }

    public int sumRange(int left, int right) {
        return left == 0 ? prefixSum[right] : prefixSum[right] - prefixSum[left - 1];
    }
}
